﻿/// <reference path="G:\WEB FORM\tiengnhatthatde\SQT.tracnghiem\Ajax/ASP/Letter/return_view_mail.aspx" />
$(document).ready(function () {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx", true);
    xhttp.send();
    
    $("#ddl_speciesEmail").val(1);

    var xhttp;

    var species = $("#ddl_speciesEmail").val();
    var sendedEmail = $("#ddl_sendedEmail").val();
    var filter = $("#ddl_filterEmail").val();

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
            $("#email_info_contact").html("");
            countEmail();
        }
    };

    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx?ddl_speciesEmail=" + species + "&&ddl_filterEmail=" + filter + "&&ddl_sendedEmail=" + sendedEmail, true);
    xhttp.send();

})

function get_mail_info_contact(email_id) {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("email_info_contact").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_info_contact.aspx?email_id=" + email_id, true);
    xhttp.send();
}

function get_customer_mail_info_contact(email_id) {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("email_info_contact").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_customer_email_info_contact.aspx?email_id=" + email_id, true);
    xhttp.send();
}

function get_feedback_mail_info_contact(email_id) {
    var xhttp;
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("email_info_contact").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_feedback_email_info_contact.aspx?email_id=" + email_id, true);
    xhttp.send();
}

function send_mail() {
    var mailContent = tinymce.get('letter-answer').getContent();
    var clientEmail = $("#Client_Email").html();
    var email_id = $("#Email_ID").html();
    var kind_of_a_mail = $("#ddl_speciesEmail").val();
    var date_of_send = new Date().toLocaleString();
    if (clientEmail != "" && mailContent != "") {
        $.post("/Ajax/ASP/Letter/email_sender.aspx", { EmailContent: escape(mailContent), ClientEmail: clientEmail, Email_ID: email_id, Species: kind_of_a_mail, DateOfSend: date_of_send });
        tinyMCE.activeEditor.setContent('');
        if (kind_of_a_mail == 1) {
            $("#email_contact_id_" + email_id + " td i").attr("class", "fa fa-plane");
        }
        if (kind_of_a_mail == 2) {
            $("#email_feedback_contact_id_" + email_id + " td i").attr("class", "fa fa-plane");
        }
        if (kind_of_a_mail == 3) {
            $("#email_customer_contact_id_" + email_id + " td i").attr("class", "fa fa-plane");
        }
        successNotification();
    }
    else {
        errorNotification();
        return;
    }
}

$(document).on('change', "#ddl_speciesEmail", function () {
    //alert($(this).val())
    var xhttp;

    var species = $("#ddl_speciesEmail").val();
    var sendedEmail = $("#ddl_sendedEmail").val();
    var filter = $("#ddl_filterEmail").val();

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
            $("#email_info_contact").html("");

            countEmail();
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx?ddl_speciesEmail=" + species + "&&ddl_filterEmail=" + filter + "&&ddl_sendedEmail=" + sendedEmail, true);
    xhttp.send();
});

$(document).on('change', "#ddl_sendedEmail", function () {
    var xhttp;

    var species = $("#ddl_speciesEmail").val();
    var sendedEmail = $("#ddl_sendedEmail").val();
    var filter = $("#ddl_filterEmail").val();

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
            $("#email_info_contact").html("");

            countEmail();
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx?ddl_speciesEmail=" + species + "&&ddl_filterEmail=" + filter + "&&ddl_sendedEmail=" + sendedEmail, true);
    xhttp.send();
});

$(document).on('change', "#ddl_filterEmail", function () {

    var xhttp;

    var species = $("#ddl_speciesEmail").val();
    var sendedEmail = $("#ddl_sendedEmail").val();
    var filter = $("#ddl_filterEmail").val();

    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
            $("#email_info_contact").html("");
            countEmail();
        }
    };

    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx?ddl_speciesEmail=" + species + "&&ddl_filterEmail=" + filter + "&&ddl_sendedEmail=" + sendedEmail, true);
    xhttp.send();
});

$(document).on('keyup', "#txt_searchEmail", function () {
    var xhttp;
    var searchText = $("#txt_searchEmail").val();
    var species = $("#ddl_speciesEmail").val();
    var sendedEmail = $("#ddl_sendedEmail").val();
    var filter = $("#ddl_filterEmail").val();
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("get_email_contact").innerHTML = this.responseText;
            countEmail();
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_email_contact.aspx?ddl_speciesEmail=" + species + "&&ddl_sendedEmail=" + filter + "&&ddl_filterEmail=4&&searchText=" + searchText, true);
    xhttp.send();
})

function
countEmail() {
    var numOfShowRows = 0;
    numOfShowRows = $('table tbody tr').filter(function () {
        return $(this).css('display') !== 'none';
    }).length;
    if ($("#ddl_speciesEmail").val() != 0) {
        $("#lbl_countKOM").html(countKOM());
        $("#lbl_countValue").html(numOfShowRows + " thư");
    }
    else {
        $("#lbl_countKOM").html("Danh sách thư");
        $("#lbl_countValue").html("");
    }
}

function countKOM() {
    var species = $("#ddl_speciesEmail").val();
    if (species == 1)
        return "Đăng ký mở lớp: ";
    if (species == 2)
        return "Góp ý: ";
    if (species == 3)
        return "Đăng ký nhận tin: ";
}

$(document).ready(function () {
    countEmail();
});

function successNotification() {
    $("#success-modal").show();
    document.getElementById("success-modal").getElementsByClassName("success_content")[0].innerHTML = "Gửi mail thành công!";
    setTimeout("$('#success-modal').fadeOut()", 5000);
}

function errorNotification() {
    $("#error-modal").show();
    document.getElementById("error-modal").getElementsByClassName("error_content")[0].innerHTML = "Không thành công!";
    setTimeout("$('#error-modal').fadeOut()", 5000);
}

$(document).on('click', ".letter-list-table tbody tr", function () {
    $(".letter-list-table tbody tr").removeClass("w3-teal");
    $(this).addClass("w3-teal");
    $(this).removeClass("notViewed");
    $(this).addClass("Viewed");
});

function history_mail(btn) {
    if ($(".letter-detail").hasClass("linuxed")) {
        $(".letter-detail").css("height", "516px");
        $(".letter-detail").removeClass("linuxed");
        $(".letter-history").css("display", "none");
        $("#reply-mail").css("display", "inline-block");
        $("#return-view").css("display", "inline-block");
        $(btn).find("i").removeClass("fa-chevron-circle-down").addClass("fa-history");
        $(btn).find("span").html("Lịch sử");
    }
    else {
        $(".letter-detail").addClass("linuxed");
        $(".letter-detail").css("height", "140px");
        $(".letter-history").css("display", "block");
        $("#reply-mail").css("display", "none");
        $("#return-view").css("display", "none");
        $(btn).find("i").removeClass("fa-history").addClass("fa-chevron-circle-down");
        $(btn).find("span").html("Đóng");

        get_mail_history();
    }
}

function get_mail_history() {
    var xhttp;
    var species = $("#ddl_speciesEmail").val();
    var email_id = $("#Email_ID").html();
    xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            document.getElementById("letter-history-view").innerHTML = this.responseText;
        }
    };
    xhttp.open("GET", "/Ajax/ASP/Letter/get_history_mail_sended.aspx?Species=" + species + "&&Email_ID=" + email_id, true);
    xhttp.send();
}
function reply_mail(btn) {
    if ($(".letter-detail").hasClass("linuxed")) {
        $(".letter-detail").css("height", "516px");
        $(".letter-detail").removeClass("linuxed");
        $(".letter-answer").css("display", "none");
        $(".letter-send-btn").css("display", "none");
        $("#letter-history").css("display", "inline-block");
        $("#return-view").css("display", "inline-block");
        $(btn).find("i").removeClass("fa-chevron-circle-down").addClass("fa-chevron-circle-up");
        $(btn).find("span").html("Trả lời");
    }
    else {
        $(".letter-detail").addClass("linuxed");
        $(".letter-detail").css("height", "140px");
        $(".letter-answer").css("display", "block");
        $(".letter-send-btn").css("display", "inline-block");
        $("#letter-history").css("display", "none");
        $("#return-view").css("display", "none");
        $(btn).find("i").removeClass("fa-chevron-circle-up").addClass("fa-chevron-circle-down");
        $(btn).find("span").html("Đóng");
    }
}

function return_view() {
    var email_id = $("#Email_ID").html();;
    var kind_of_a_mail = $("#ddl_speciesEmail").val();
    $.post("/Ajax/ASP/Letter/return_view_mail.aspx", { Email_ID: email_id, Species: kind_of_a_mail });
    rviewNotification();
    if (kind_of_a_mail == 1) {
        $("#email_contact_id_" + email_id).attr("class", "notViewed");
    }
    if (kind_of_a_mail == 2) {
        $("#email_feedback_contact_id_" + email_id).attr("class", "notViewed");
    }
}
function rviewNotification() {
    $("#return-view-success-modal").show();
    document.getElementById("return-view-success-modal").getElementsByClassName("rview_content")[0].innerHTML = "Chuyển về chưa xem!";
    setTimeout("$('#return-view-success-modal').fadeOut()", 1000);
}